package com.zero.zeroframe.v

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.zero.zeroframe.utils.BindingReflex

/**
 * Fragment基类
 * @author Zero
 * @since 2/4/24
 */
abstract class BaseFragment<VB : ViewBinding> : Fragment(){

    /**
     * 私有的 ViewBinding 此写法来自 Google Android 官方
     */
    private var _binding: VB? = null
    protected val mBinding get() = _binding!!
    protected var isFirstLoad = true

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        isFirstLoad = true
        _binding = BindingReflex.reflexViewBinding(javaClass, layoutInflater)
        return _binding?.root
    }

    //懒加载
    override fun onResume() {
        super.onResume()

        if (isFirstLoad){
            isFirstLoad = false
            _binding?.initView()
            initData()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    abstract fun VB.initView()
    open fun initData() {}

    /**
     * 获取当前Fragment状态
     * @return true为正常 false为未加载或正在删除
     */
    open fun getStatus(): Boolean {
        return isAdded && !isRemoving
    }


}